//
//  BRMyDeviceCell.m
//  Kexu
//
//  Created by IOS on 2023/8/1.
//

#import "BRMyDeviceCell.h"

@implementation BRMyDeviceCell


- (void)br_text:(NSString *)text by:(NSInteger)index {
    BRStackTitleTextView *titleStack = [self viewWithTag:1000 + index];
    titleStack.text = text;
}

- (void)setDto:(BRDeviceModel *)dto {
    _dto = dto;
    
    self.slotView.stackTitles.title = [NSString stringWithFormat:@"%ld",dto.deviceCount];
    
    self.nameL.text = dto.cabinetNo;
    self.imeiL.text = [NSString stringWithFormat:@"%@: %@",Localized(@"shop_imei"),dto.imei];
    
    [self br_text:[NSString stringWithFormat:@"%ld",dto.availableNum] by:0];
    [self br_text:[NSString stringWithFormat:@"%ld",dto.returnableNum] by:1];
    [self br_text:[NSString stringWithFormat:@"%ld",dto.damagedNum] by:2];
    [self br_text:[NSString stringWithFormat:@"%ld",dto.abnormalCount] by:3];
    
    
    NSInteger state = dto.customizeState;
    if (state != 2) {//非锁定状态
        state = dto.state;
    }
    [self.statusView br_status:state];
}


#pragma mark - UI --
- (void)setUI {
    [super setUI];
    self.contView.backgroundColor = UIColor.whiteColor;
    [self.contView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.offset(-10);
    }];
    
    self.topView.backgroundColor = UIColorClear;
    self.centView.backgroundColor = UIColorClear;
    
    UIStackView *nameStack = [[UIStackView alloc] init];
    nameStack.spacing = 4;
    nameStack.axis = UILayoutConstraintAxisVertical;
    [self.topView addSubview:nameStack];
    [nameStack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(76);
        make.centerY.offset(0);
        make.right.lessThanOrEqualTo(self.topView.mas_right).offset(-90);
    }];
    
    [nameStack addArrangedSubview:self.nameL];
    [nameStack addArrangedSubview:self.imeiL];
    
    [self.topView addSubview:self.statusView];
    [self.statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.height.mas_equalTo(26);
//        make.centerY.offset(0);
        make.top.offset(22.5);
        make.bottom.offset(-22.5);
    }];
    
    BRSlotsView *slotView = [[BRSlotsView alloc] init];
    slotView.layer.cornerRadius = 26;
    [self.topView addSubview:slotView];
    [slotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(52);
        make.left.offset(14);
        make.centerY.offset(0);
    }];
    _slotView = slotView;
    //---------------
    UIView *centerV = [UIView new];
    [self.centView addSubview:centerV];
    [centerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.height.mas_equalTo(80);
    }];
    
    UIView *line = [BRToolsView getLineView];
    [centerV addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.mas_equalTo(0.5);
        make.top.offset(0);
    }];
    
    NSArray *titles = @[Localized(@"shop_available"),Localized(@"shop_empty"),Localized(@"shop_faulty"),Localized(@"shop_exceptional")];
    CGFloat w = (kScreenWidth - 28) / titles.count;
    for (int i = 0; i < titles.count; i++) {
        BRStackTitleTextView *titleStack = [[BRStackTitleTextView alloc] init];
        titleStack.title = titles[i];
        titleStack.tag = 1000 + i;
        [centerV addSubview:titleStack];
        [titleStack mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(w);
            make.left.offset(w * i);
            make.centerY.offset(0);
        }];
    }
    
}

- (QMUILabel *)nameL {
    
    if (_nameL == nil) {
        _nameL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontB:17]];
    }
    return _nameL;
}

- (QMUILabel *)imeiL {
    if (_imeiL == nil) {
        _imeiL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:12]];
    }
    return _imeiL;

}

- (BRMStateV2View *)statusView {
    
    if (_statusView == nil) {
        _statusView = [[BRMStateV2View alloc] init];
    }
    return _statusView;
}

@end
